package com.ice.app.system.menu.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.util.StringUtil;
import com.ice.app.system.entity.Menu;
import com.ice.app.system.mapper.MenuMapper;
import com.ice.app.system.menu.vo.MenuTreeVo;
import com.iceframework.core.utils.KeyUtils;
import com.iceframework.core.utils.TreeParserUtils;

/**
 * 菜单
 * 
 * @author Palo
 *
 */
@Service
public class MenuService {

	@Autowired
	private MenuMapper menuMapper;

	// ----------------------------------------自动生成--------------------------------------------

	/**
	 * 菜单保存
	 * 
	 * @param menu
	 * @return
	 * @throws Exception
	 */
	@Transactional
	public boolean saveMenu(Menu menu) throws Exception {
		if (StringUtil.isNotEmpty(menu.getMenuId())) {
			// 修改操作
			Menu old = this.getMenu(menu.getMenuId());
			menu.setUpdateBy("admin");
			menu.setUpdateDate(new Date());
			if (old != null) {
				return menuMapper.updateByPrimaryKeySelective(menu) > 0;
			}
			return false;
		} else {
			// 保存操作
			String key = KeyUtils.genKey("Menu");
			menu.setMenuId(key);
			menu.setCreateBy("admin");
			menu.setCreateDate(new Date());
			menu.setUpdateBy("admin");
			menu.setUpdateDate(new Date());
			return menuMapper.insertSelective(menu) > 0;
		}
	}

	/**
	 * 根据主键删除菜单
	 * 
	 * @param key
	 * @return
	 */
	@Transactional
	public boolean deleteMenu(String key) {
		deleteChildMenusByMenuId(key);
		return menuMapper.deleteByPrimaryKey(key) > 0;
	}

	/**
	 * 根据多个主键删除菜单
	 * 
	 * @param keys
	 * @return
	 */
	@Transactional
	public boolean deleteMenus(String keys) {
		if (!StringUtils.isEmpty(keys)) {
			String[] strs = keys.split(",");
			for (String key : strs) {
				this.deleteMenu(key);
			}
			return true;
		}
		return false;
	}

	/**
	 * 根据主键获取菜单实体
	 * 
	 * @param key
	 * @return
	 */
	public Menu getMenu(String key) {
		return menuMapper.selectByPrimaryKey(key);
	}

	/**
	 * 根据给定条件获取菜单实体集合
	 * 
	 * @param key
	 * @return
	 */
	public List<Menu> getMenus(Menu menu) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("menu", menu);
		return menuMapper.selectByFilter(map);
	}

	// ----------------------------------------自定义（Public）---------------------------------------------

	/**
	 * 根据parentId获取菜单(包含按钮) 有效和无效都取
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Menu> getListParentId(String parentId) {
		return menuMapper.selectByParentId(parentId);
	}

	/**
	 * 获取菜单树
	 * 
	 * @return
	 */
	public List<MenuTreeVo> getMenuTree(String userName) {
		// 获取目录
		List<Menu> content = menuMapper.selectContent(userName);

		List<MenuTreeVo> menuTree = new ArrayList<MenuTreeVo>();
		List<Menu> allNoButtomMenus = menuMapper.selectNotButtonList(userName);
		for (Menu menu : content) {
			MenuTreeVo temp = new MenuTreeVo();
			temp.setId(menu.getMenuId());
			temp.setParentId(menu.getParentId());
			temp.setTitle(menu.getName());
			temp.setHref(menu.getUrl());
			temp.setI18n(menu.getI18n());
			temp.setIcon(menu.getIcon());
			temp.setOpenWith(menu.getOpenWith());
			temp.setSpread(false);
			if (allNoButtomMenus != null) {
				List<MenuTreeVo> menuTreeVo = transMenuTreeVo(allNoButtomMenus);
				List<MenuTreeVo> trees = TreeParserUtils.getTreeList(temp.getId(), menuTreeVo);
				temp.setChildren(trees);
			}
			menuTree.add(temp);
		}
		return menuTree;
	}

	// ----------------------------------------自定义(Private)------------------------------------------

	/**
	 * 转换TreeVo
	 * 
	 * @param menus
	 * @return
	 */
	private List<MenuTreeVo> transMenuTreeVo(List<Menu> menus) {
		if (menus != null && menus.size() != 0) {
			List<MenuTreeVo> trees = new ArrayList<>();
			for (Menu menu : menus) {
				MenuTreeVo temp = new MenuTreeVo();
				temp.setId(menu.getMenuId());
				temp.setParentId(menu.getParentId());
				temp.setI18n(menu.getI18n());
				temp.setTitle(menu.getName());
				temp.setHref(menu.getUrl());
				temp.setIcon(menu.getIcon());
				temp.setOpenWith(menu.getOpenWith());
				temp.setSpread(false);
				trees.add(temp);
			}
			return trees;
		}
		return null;
	}

	/**
	 *	获取菜单树
	 * @param role
	 * @return
	 */
	public List<MenuTreeVo> getMenuTreeByRoleIdMenuType(String roleId,String menuType){
		List<MenuTreeVo> menu = menuMapper.selectResourceTreeByRoleIdAndMenuType(roleId,menuType);
		MenuTreeVo menuTreeVo = new MenuTreeVo();
		menuTreeVo.setName("菜单");
		menuTreeVo.setId("0");
		menuTreeVo.setChecked(false);
		menu.add(menuTreeVo);
		return menu;
	}
	
	/**
	 *	获取菜单树
	 * @param role
	 * @return
	 */
	public List<MenuTreeVo> getMenuZTreeByUserId(String userId){
		List<MenuTreeVo> menu = menuMapper.selectZTreeByUserId(userId);
		MenuTreeVo menuTreeVo = new MenuTreeVo();
		menuTreeVo.setName("菜单");
		menuTreeVo.setId("0");
		menuTreeVo.setChecked(false);
		menu.add(menuTreeVo);
		return menu;
	}

	/**
	 * 根据菜单ID删除菜单
	 * @param menuId
	 */
	private void deleteChildMenusByMenuId(String menuId){
		if(StringUtils.isNotEmpty(menuId)){
			Menu menu = new Menu();
			menu.setParentId(menuId);
			List<Menu>  menus = this.getMenus(menu);
			for (int i = 0; i < menus.size(); i++) {
				this.deleteMenu(menus.get(i).getMenuId());
			}
		}
	}

}
